#include "BaseEmitter.hpp"
#include "EmitterManager.hpp"

using namespace std;

EmitterManager* EmitterManager::m_pInstance = NULL;

EmitterManager* EmitterManager::GetInstance()
{
	if(!m_pInstance)
	{
		m_pInstance = new EmitterManager();
	}

	return m_pInstance;
}

void EmitterManager::DestroyInstance()
{
	delete m_pInstance;
	m_pInstance = NULL;
}

EmitterManager::EmitterManager()
{
}

EmitterManager::~EmitterManager()
{
	vector<BaseEmitter*>::iterator currEmitter = m_pEmitters.begin();
	vector<BaseEmitter*>::iterator endOfEmitters = m_pEmitters.end();

	for(currEmitter; currEmitter != endOfEmitters; ++currEmitter)
	{
		delete *currEmitter; *currEmitter = NULL;
		m_pEmitters.erase(currEmitter);
	}
}

void EmitterManager::AddEmitter(BaseEmitter *emitter)
{
	m_pEmitters.push_back(emitter);
}

void EmitterManager::Update(clock_t tick)
{
	vector<BaseEmitter*>::iterator currEmitter = m_pEmitters.begin();
	vector<BaseEmitter*>::iterator endOfEmitters = m_pEmitters.end();

	for(currEmitter; currEmitter != endOfEmitters; ++currEmitter)
		(*currEmitter)->Update(tick);
}

void EmitterManager::Render()
{
	vector<BaseEmitter*>::iterator currEmitter = m_pEmitters.begin();
	vector<BaseEmitter*>::iterator endOfEmitters = m_pEmitters.end();

	for(currEmitter; currEmitter != endOfEmitters; ++currEmitter)
		(*currEmitter)->Render();
}

void EmitterManager::removeEmitter(BaseEmitter* pEmitter)
{
	vector<BaseEmitter*>::iterator currEmitter = m_pEmitters.begin();
	vector<BaseEmitter*>::iterator endOfEmitters = m_pEmitters.end();

	for(currEmitter; currEmitter != endOfEmitters; ++currEmitter)
	{
		if((*currEmitter) == pEmitter)
		{
			delete pEmitter;
			m_pEmitters.erase(currEmitter);
			break;
		}
	}
}

//EOF
